//
//  ChipInstallDetailViewController.m
//  CarPark
//
//  Created by oceans on 2019/8/5.
//  Copyright © 2019 oceans. All rights reserved.
//

#import "ChipInstallDetailViewController.h"
#import "WatchHeaderCollectionReusableView.h"
#import "ChipInstallDetailCollectionViewCell.h"
#import "ChipInstallViewController.h"

@interface ChipInstallDetailViewController () <UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>

@property (strong, nonatomic) UICollectionView *collection;
@property (strong, nonatomic) NSDictionary *info;
@end

@implementation ChipInstallDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"任务详情";
    [self loadData];
}

- (void)loadData {
    
    defweakself
    [HttpRequest getURL:ktask_detail paras:@{@"type": @"task_chip_install", @"id": [NSString stringWithFormat:@"%d", __id]} showProgress:NO successful:^(id  _Nullable result) {
        NSLog(@"%@", result);
        weakself.info = result[@"datas"];
        [weakself setupView];
    } failed:^(int code, NSString * _Nullable errMsg) {
        
    }];
}

- (void)setupView {
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    self.collection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, XNavigationHeight, ScreenWidth, ScreenHeight-XNavigationHeight-XBotSafeSpace-40.0) collectionViewLayout:layout];
    _collection.delegate = self;
    _collection.dataSource = self;
    _collection.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_collection];
    [_collection registerNib:[UINib nibWithNibName:@"ChipInstallDetailCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"ChipInstallDetailCollectionViewCell"];
    [_collection registerNib:[UINib nibWithNibName:@"WatchHeaderCollectionReusableView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"WatchHeaderCollectionReusableView"];
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.titleLabel.font = [UIFont systemFontOfSize:15.0];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [button setTitle:@"开始安装" forState:UIControlStateNormal];
    button.frame = CGRectMake(0, ScreenHeight-XBotSafeSpace-40.0, ScreenWidth, 40.0+XBotSafeSpace);
    [button addTarget:self action:@selector(start) forControlEvents:UIControlEventTouchUpInside];
    button.backgroundColor = COLOR(101, 101, 101, 1);
    [self.view addSubview:button];
}

- (void)start {
    
    ChipInstallViewController *con = [[ChipInstallViewController alloc] init];
    con.info = _info;
    [self.navigationController pushViewController:con animated:YES];
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    
    return CGSizeMake(ScreenWidth, 45.0);
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        
        WatchHeaderCollectionReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"WatchHeaderCollectionReusableView" forIndexPath:indexPath];
        view.rightImageView.hidden = YES;
        if (indexPath.section == 0) {
            view.titleLabel.text = @"任务信息";
        }
        return view;
    }
    return nil;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    
    return 0.0;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    
    return 0.0;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    return CGSizeMake(ScreenWidth, 40.0);
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return 5;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    ChipInstallDetailCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ChipInstallDetailCollectionViewCell" forIndexPath:indexPath];
    if (indexPath.item == 0) {
        cell.leftLabel.text = @"截止时间";
        cell.rightLabel.text = _info[@"etc_time"];
    }else if (indexPath.item == 1) {
        cell.leftLabel.text = @"车辆所属";
        cell.rightLabel.text = _info[@"dealer"][@"name"];
    }else if (indexPath.item == 2) {
        cell.leftLabel.text = @"停车场";
        cell.rightLabel.text = _info[@"parking"][@"name"];
    }else if (indexPath.item == 3) {
        cell.leftLabel.text = @"安装数量";
        cell.rightLabel.text = [NSString stringWithFormat:@"(%@/%@)", _info[@"autos_num"][@"install_nums"], _info[@"autos_num"][@"all_nums"]];
    }else if (indexPath.item == 4) {
        cell.leftLabel.text = @"创建时间";
        cell.rightLabel.text = _info[@"created_at"];
    }
    return cell;
}

@end
